﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using GameBase;

namespace GalaxyPresident
{
    public abstract class PlanetObjectOwner : MutiSpriteObject
    {
        protected Planet _planetOwner;
        protected Sprite sprite;
        protected Sprite sprite_s;

        protected float meterialCost;
        protected float moneyCost;
        protected float energyCost;

        protected float _healthPoint;
        protected float _defense;
        protected VisibleObject _explosion;
        protected bool _isExplosion = false;

        protected List<int> idleSequence;
        protected List<int> workSequence;
        protected List<int> collapseSequence;

        protected List<int> idleSequence_s;
        protected List<int> workSequence_s;
        protected List<int> collapseSequence_s;

        public Planet PlanetOwner
        {
            get { return _planetOwner; }
            set { _planetOwner = value; }
        }

        public PlanetObjectOwner(Planet planet)
        {
            _planetOwner = planet;
            initInfo();
        }

        public void updatePlanetOwnerResources()
        {
            _planetOwner.Meterial -= meterialCost;
            _planetOwner.Energy -= energyCost;

            if (_planetOwner.TeamOwner != null)
            {
                _planetOwner.TeamOwner.TotalMoney -= moneyCost;
            }
        }

        protected virtual void initInfo()
        {
        }

        public void beHurtWithPower(float power)
        {
            if (_healthPoint <= 0)
                return;

            _healthPoint -= power * (1 - _defense);
            if (_healthPoint <= 0)
                destroyProcessing();
        }

        protected virtual void destroyProcessing()
        {
            if (!_isExplosion)
            {
                _isExplosion = true;
                _explosion.Position = Position;
                _planetOwner.ViewLayer.AddChild(_explosion);
            }
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            if (_isExplosion)
            {
                _currentTotalTime += gameTime.ElapsedGameTime.Milliseconds;
                if (_currentTotalTime > 200)
                {

                    Visible = false;
                }
                if (_currentTotalTime >= 1000)
                {
                    _currentTotalTime = 0;
                    _planetOwner.ViewLayer.RemoveChild(_explosion);
                    _planetOwner.ViewLayer.RemoveChild(this);
                    removeObjectWhenDestroy();
                }
            }
        }

        protected virtual void removeObjectWhenDestroy()
        {
            
        }

        public bool isEnoughResourcesForProduce(Planet planet)
        {
            if (planet.TeamOwner != null)
            {
                if (planet.Meterial >= meterialCost
                    && planet.Energy >= energyCost
                    && planet.TeamOwner.TotalMoney >= moneyCost)
                    return true;
            }
            return false;
        }

        public bool isMyPlanetEnoughResourcesForProduce()
        {
            return isEnoughResourcesForProduce(_planetOwner);
        }

        public virtual string getInfoString()
        {
            return null;
        }

        public string getCostString()
        {
            return "Material: " + ((int)meterialCost).ToString()
                 +"\nEnergy  : " + ((int)energyCost).ToString()
                + "\nMoney   : " + ((int)moneyCost).ToString();
        }
    }
}
